'use strict';

angular.module('app')
    .controller('updateController', ['$scope', '$rootScope', '$state', '$timeout',
        function($scope, $rootScope, $state, $timeout) {
            var title = "";
            $scope.imgData={};
            $scope.url="https://ifci.lincomb.com";//本地也显示图片
            if($state.includes('**.notice.update')){
                title="编辑头条内容";
                var noticeNo = $state.params.noticeNo;
                detail(noticeNo);
                $scope.isDisabled = false;
            }else if($state.includes('**.notice.add')){
             $scope.startTime ='请选择开始时间';
             $scope.endTime ='请选择结束时间';
                title="添加头条内容";
                $scope.isDisabled = false;
            }
            $scope.title = $rootScope.title = title;
            $scope.loading = true;

            //初始化验证
            $scope.submit= function(){
                $scope.loading = true;

                if(!$('#noticeTitle').val()){
                    $scope.alertModal({'content':'头条标题不能为空'});
                    return;
                }

//                if(!$('#requestUrl').val()){
//                    $scope.alertModal({'content':'跳转地址不能为空'});
//                    return;
//                }
                if(!$('#showFlag').val()){
                    $scope.alertModal({'content':'头条展示类型不能为空'});
                    return;
                }

                 if($('#startTimeOne').val()){
                $scope.record.startTime =$('#startTimeOne').val();
                }
                 if($('#endTimeOne').val()){
                 $scope.record.endTime =$('#endTimeOne').val();
                 }
                if($scope.record.startTime == ""||$scope.record.startTime==null ){
                    $scope.alertModal({'content':'请选择开始时间'});
                    return;
                }
                if($scope.record.endTime == ""||$scope.record.endTime==null ){
                   $scope.alertModal({'content':'请选择结束时间'});
                   return;
                }

                 if( $scope.record.startTime > $scope.record.endTime){
                      $scope.alertModal({'content': '开始时间不能大于结束时间'});
                      return;
                  }
                var picUrl = $scope.imgData[0];
                if (!picUrl) {
                    $scope.alertModal({'content':'请上小ico'});
                    return;
                }
                var picUrl = $scope.imgData[0].src;
                $scope.record.smaillIcon = picUrl[0];
                saveData();
            };

            function saveData(){
                var m = $scope.record;
                console.log(m);
                if(m){
                    $scope.isDisabled = true;//提交disabled
                    $.ajax({
                        url : $scope.record.noticeNo ? $scope.app.project_name+'/notice/update' : $scope.app.project_name+'/notice/add',
                        data: m
                    }).then(callback);
                }
                function callback(result){
                   console.log(result)
                    if(result.httpCode ==200){
                        $scope.alertModal({"content":"操作成功"});
                        $timeout(function(){
                            $state.go('main.headlines.notice.list');
                        },2000);
                    }else{
                     $scope.msg = result.msg;
                       $scope.alertModal({"content": $scope.msg });
                     $timeout(function(){
                         $state.go('main.headlines.notice.list');
                     },2000);
                    }
                }
            }

            // 初始化页面
            function detail(id,roleId) {
                $scope.loading = true;
                $.ajax({
                    url : $scope.app.project_name + '/notice/detail',
                    data: {'noticeNo': noticeNo}
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.record = result.data;
                          $scope.startTime =$scope.record.startTime ;
                          $scope.endTime =$scope.record.endTime ;
                           console.log($scope.record)
                        $scope.imgData = [];
                        var srcArr = [];
                        srcArr.push($scope.record.smaillIcon);
                        $scope.imgData.push({
                            src:srcArr
                        })

                        console.log($scope.imgData)
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });

            }

        }]);